package com.zhdl.modules.common.protocol_self.service.impl;

import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.common.protocol_self.entity.ProtocolDemo;
import com.zhdl.modules.common.protocol_self.repository.ProtocolDemoRepository;
import com.zhdl.modules.common.protocol_self.service.ProtocolDemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ProtocolDemoServiceImpl extends BaseServiceImpl<Integer, ProtocolDemo, ProtocolDemoRepository> implements ProtocolDemoService {

    @Resource
    private ProtocolDemoRepository protocolDemoRepository;

    @Override
    public ProtocolDemo addProtocol(ProtocolDemo protocolDemo) {
        if (protocolDemo.getId() == null) {
            return protocolDemoRepository.save(protocolDemo);
        } else {
            return new ProtocolDemo();
        }
    }

    @Override
    public ProtocolDemo updateProtocol(ProtocolDemo protocolDemo) {
        if (protocolDemo.getId() != null) {
            return protocolDemoRepository.save(protocolDemo);
        } else {
            return new ProtocolDemo();
        }
    }

    @Override
    public void deleteProtocol(Integer id) {
        protocolDemoRepository.deleteById(id);
    }

    @Override
    public ProtocolDemo findByIdProtocol(Integer id) {
        Optional<ProtocolDemo> byId = protocolDemoRepository.findById(id);
        return byId.orElseGet(ProtocolDemo::new);
    }

    @Override
    public List<ProtocolDemo> findAllProtocol() {
        return protocolDemoRepository.findAll();
    }

}
